<?
     include "../../common.php" ;
     $opt = $_POST['opt'] ;
     $arr = [] ;
     if( $opt == 'getList' ){
        $page = $_POST['page'] ;
        $perPage = $_POST['perPage'] ;
        $pageSt = ( $page - 1 ) * $perPage ; 
        $sql = "SELECT p.*,c.cateName,c.subid,c.subCateName,t.typeName FROM `products` AS p JOIN pcate AS c ON p.cateid = c.id JOIN ptype AS t ON p.typeid = t.id WHERE is_delete = 1 ORDER BY p.id ASC LIMIT {$pageSt},{$perPage}" ;
        $res = mysqli_query($link,$sql);
        while( $one = mysqli_fetch_assoc($res) ){
            $arr[] = $one ; 
        }
     }
     if( $opt == 'addPro' ){
        $proName = $_POST['proName'] ; 
        $cateId = $_POST['cateId'] ; 
        $typeId = $_POST['typeId'] ; 
        $price = $_POST['price'] ; 
        $inventory = $_POST['inventory'] ; 
        $discount = $_POST['discount'] ; 
        $now =Time();
        $str = date('Ymd',Time()) ;
        if(!file_exists("../../../uploads/{$str}")){     //看是否存在文件夹uploads
            mkdir("../../../uploads/{$str}");
        }
        $path = $_FILES['imgSrc']['tmp_name'];
        move_uploaded_file($path,"../../../uploads/{$str}/"."{$now}.jpg");
        $sql = "INSERT INTO `products`(`pname`, `price`, `cateid`, `typeid`, `img`, `discount`, `inventory`) VALUES ('{$proName}','{$price}',{$cateId},{$typeId},'{$str}/{$now}.jpg','{$discount}','{$inventory}')" ;
        $res = mysqli_query($link,$sql);
        $arr['sql'] = $sql ;
        if( $res ){
            $arr['status'] = 1 ; 
            $arr['info'] = '添加成功!' ;
        } else {
            $arr['status'] = 0 ; 
            $arr['info'] = '操作失败!' ;
        }
    }
     if( $opt == 'delPro' ){
        $myid = $_POST['myid'] ;
        $sql = "UPDATE `products` SET is_delete = 2 WHERE id = {$myid}" ; 
        $res = mysqli_query($link,$sql);
        if( $res ){
            $arr['status'] = 1 ;
            $arr['info'] = '删除成功!' ;
        } else {
            $arr['status'] = 0 ;
            $arr['info'] = '操作失败!' ;
        }
     }
     if( $opt == 'getPage' ){
        $perPage = $_POST['perPage'] ;
        $sql = "SELECT COUNT(*) AS c FROM `products` WHERE is_delete = 1" ; 
        $res = mysqli_query($link,$sql);
        if( $one = mysqli_fetch_assoc($res) ){
            $pageNum = $one['c'] ;
            $arr['totalPage'] = ceil( $pageNum / $perPage) ; 
        }
     }
     if( $opt == 'selAddPro' ){
        $sql1 = "SELECT * FROM `ptype` ORDER BY id ASC" ;
        $res1 = mysqli_query($link,$sql1);
        $sql2 = "SELECT * FROM `pcate` WHERE subid IS NOT NULL ORDER BY id ASC" ;
        $res2 = mysqli_query($link,$sql2);
        while( $one1 = mysqli_fetch_assoc($res1) ){
            $arr1[] = $one1 ;
        } 
        while( $one2 = mysqli_fetch_assoc($res2) ){
            $arr2[] = $one2 ;
        }
        foreach( $arr1 as $k => $val ){
            $str1 .= "<option value='{$val["id"]}'>{$val['typeName']}</option>" ;
        }
        foreach( $arr2 as $key => $value ){
            $str2 .= "<option value='{$value["id"]}'>{$value['subCateName']}</option>" ;
        }
        $arr['data1'] = $str1 ;
        $arr['data2'] = $str2 ; 
     }
     echo json_encode($arr);
?>